L1-103 整数的持续性

题目 L1-103 整数的持续性

image-dd75ae62

思路分析

代码实现

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'

using ll = long long;

using ull = unsigned long long;

using PII = pair<int,int>;

using Pll = pair<ll,ll>;

int dx[4]= {-1,0,1,0},dy[4]= {0,1,0,-1};

const int inf = 0x3f3f3f3f;

int calc(int n) {

	int sum=1;

	while(n) {

		sum*=n%10;

		n/=10;

	}

	return sum;

}

int main() {

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	int a,b;

	cin>>a>>b;

	int maxv=-inf;

	vector<int> ans;

	for(int i=a; i<=b; i++) {

		int tmp=i;

		int step=0;

		while(tmp>=10) {

			tmp = calc(tmp);

			step++;

		}

		if(step>maxv) {

			maxv=step;

			ans.clear();

			ans.push_back(i);

		} else if(step==maxv) {

			ans.push_back(i);

		}

	}

	cout<<maxv<<endl;

	bool isfirst=true;

	for(auto v:ans)	{

		if(!isfirst)	{

			cout<<" ";

		}

		cout<<v;

		isfirst=false;

	}

	return 0;

}

同类题型

视频讲解


⬅️ L1-102 兰州牛肉面 🏠 00-天梯赛 ➡️ L1-104 九宫格